package com.changzhi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.changzhi.common.constans.Constants;
import com.changzhi.common.constans.RedisConstans;
import com.changzhi.common.constants.RedisConstants;
import com.changzhi.common.exception.ServiceException;
import com.changzhi.common.mq.MqProducer;
import com.changzhi.common.util.BaseUtil;
import com.changzhi.common.util.DateUtil;
import com.changzhi.ldyl.model.*;
import com.changzhi.ldyl.model.dto.YearDto;
import com.changzhi.ldyl.model.vo.*;
import com.changzhi.mapper.*;
import com.changzhi.service.*;
import com.changzhi.util.CommonUtil;
import com.changzhi.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 */
@Slf4j
@Service
public class ChannelVIPServiceImpl implements ChannelVIPService {

	@Resource
	private SysDictService sysDictService;

	@Resource
	private RedisUtil redisUtil;

	@Resource
	private ChannelVIPMapper channelVIPMapper;

	@Resource
	private UserGiftMapper userGiftMapper;

	@Resource
	private MqProducer mqProducer;

	/**
	 * 检查是否达到房间VIP
	 */
	public void checkChannelVip(UserGift userGift) {
		ChannelVIP vip = getChannelVip(userGift);
		//已经完成VIP任务不继续
		if (null != vip) {
			return;
		}
		checkVip(userGift);

	}

	private void checkVip(UserGift userGift) {
		log.info("收到礼物--正在检测VIP信息");
		Integer diamondLimit = 10000;
		SysDict vip_diamondLimit = sysDictService.getSysDictByCache("VIP_DiamondLimit", RedisConstants.FIVE_MINUTES);
		if (null != vip_diamondLimit) {
			diamondLimit = Integer.valueOf(vip_diamondLimit.getProperty1());
		}
		String createTime = DateUtil.localDateTimeToString(userGift.getCreateTime());
		Integer treasure = userGiftMapper.userChannelTreasure(userGift.getChannelId(), userGift.getUserId(),createTime);
		String key =  userGift.getUserId() + "_" + userGift.getChannelId();
		if(!redisUtil.hHasKey(RedisConstans.USER_CHANNELVIPTREASURE,key)){
			redisUtil.hset(RedisConstans.USER_CHANNELVIPTREASURE,key,treasure,RedisConstans.ONE_DAY);
		}
		treasure = redisUtil.hincr(RedisConstans.USER_CHANNELVIPTREASURE, key, userGift.getTotalGiftValue());
		log.info("收到礼物--treasure:{}",treasure);
		if (treasure >= diamondLimit) {
			long incr = redisUtil.incr(RedisConstans.USER_CHANNELVIPFLAG + userGift.getUserId() + ":" + userGift.getChannelId(), 1, RedisConstans.ONE_HOUR);
			if (1 == incr) {
				log.info("保存和发送VIP消息");
				ChannelVIP vip = new ChannelVIP();
				vip.setChannelId(userGift.getChannelId());
				vip.setUserId(userGift.getUserId());
				vip.setCreateTime(LocalDateTime.now());
				vip.setDiamondLimit(diamondLimit);
				vip.setDiamondRecord(treasure);
				vip.setLevel(1);
				channelVIPMapper.insert(vip);
				//发送MQ消息
				mqProducer.sendChannelVipInfo(JSONObject.toJSONString(vip));
			}


		}

	}

	private ChannelVIP getChannelVip(UserGift userGift) {
		ChannelVIP vip = null;
		if (StringUtils.isNotBlank(userGift.getUserId()) && StringUtils.isNotBlank(userGift.getChannelId())) {
			String key = RedisConstants.USER_CHANNEL_VIP + userGift.getUserId() + ":" + userGift.getChannelId();
			if (redisUtil.hasKey(key)) {
				vip = (ChannelVIP) redisUtil.get(key);
			}
			if (null == vip) {
				QueryWrapper<ChannelVIP> vipwrapper = new QueryWrapper<ChannelVIP>();
				vipwrapper.eq(ChannelVIP.USERID, userGift.getUserId());
				vipwrapper.eq(ChannelVIP.CHANNELID, userGift.getChannelId());
				vipwrapper.last("limit 1");
				vip = channelVIPMapper.selectOne(vipwrapper);
				if (null != vip) {
					redisUtil.set(key, vip, RedisConstants.ONE_DAY);
				}
			}
		}
		return vip;
	}


}
